import os
import time
import pytest
import pandas as pd
from psa.BasePage import BasePage
from psa.log import logger
from selenium.webdriver.common.by import By
from settings import DRIVER_DIR
from selenium import webdriver
from PageObject.zentao_login import LoginPage
from settings import TESTDATA_DIR



class DefaultConfig(object):
    browser_type = "chrome"
    headless = False
    url = "https://www.baidu.com/"
    


@pytest.fixture(scope='function')
def log_open_browser():
    logger.info("Opening Browser")



@pytest.fixture(scope='function')
def log_open_browser_and_login(log_open_browser):
    logger.info("login_to_psa")

@pytest.fixture(scope='function')
def transfer_args_list(request):
    logger.info("transfer args")
    params = request.param
    logger.info("transfer params Type:{}, Value:{}".format(type(params),params))
    return params

@pytest.fixture(scope='function')
def transfter_args_dict(request):
    logger.info("transfer args")
    params = request.param
    for key in params:
        logger.info("transfer param: {}, value: {}".format(key, params[key]))
    return params


@pytest.fixture(scope='function')
def browser_driver(request):
    logger.info("######################################")
    logger.info("Setup Fixture Start: {}".format(browser_driver.__name__))
    params = request.param
    logger.debug("@@@@ Open Browser With Params: {}".format(params))
    browser = params['browser_type'] if 'browser_type' in params else DefaultConfig().browser_type
    headless = params['headless'] if 'headless' in params else DefaultConfig().headless
    url = params['url'] if 'url' in params else DefaultConfig().url
    if browser == "chrome":
        driver_path = ""
        if os.name == "nt":
            driver_path = os.path.join(DRIVER_DIR, "window", "chromedriver.exe")
        elif os.name == "posix":
            driver_path = os.path.join(DRIVER_DIR, "liunx", "chromedriver")
            os.system("chmod +x {}".format(driver_path))
        options = webdriver.ChromeOptions()
        options.add_argument("--ignore-certificate-errors")
        if headless:
            options.add_argument("--headless")
        driver = webdriver.Chrome(driver_path, chrome_options=options)
        logger.info("Starting Chrome Successfully...")
        driver.maximize_window()
    elif browser == "firefox":
        options = webdriver.FirefoxOptions()
        options.add_argument("--ignore-certificate-errors")
        if headless:
            options.add_argument("--headless")
        driver = webdriver.Firefox(firefox_options=options)
        logger.info("Starting Firefox Successfully...")
        driver.maximize_window()
    else:
        logger.error("Unsupported browser {}".format(browser))
        raise ValueError("Unsupported browser {}".format(browser))
    
    try:
        driver.get(url)
        driver.maximize_window()
        logger.info("Setup Fixtrue Success:{}.".format(browser_driver.__name__))
        logger.info("######################################")
        yield driver
    except Exception as e:
        raise e
    finally:
        logger.info("######################################")
        # driver.quit()
        command = "kill $(pgrep -f {})".format(browser)
        os.system(command)
        logger.info("Close Browser Successfully By Command :{}".format(command))  
    time.sleep(3)
  
@pytest.fixture(scope="function")
def login_zentao(browser_driver,request):
    params = request.param
    username = params['username']
    password = params['password']
    login_page = LoginPage(browser_driver)
    login_page.login_success(username, password)
    return browser_driver

